package byggeTegning.husGeometri;

//import DEBUGGING.*;  // Search for DEBUG.<INFO|TRACE| ... >

import java.awt.*;
import java.awt.geom.*;
import java.util.*;

import byggeTegning.geometry.*;

// Java 3D
import javax.media.j3d.*;
import javax.vecmath.*;


// ********************************************************************
// *** CLASS: Block
// ********************************************************************
public class Block extends Object3D
{
  public static final int NORTH=1;
  public static final int EAST=3;
  public static final int SOUTH=5;
  public static final int WEST=7;
  public static final int ABOVE=9;
  public static final int BELOW=10;
  public static final int VCUT=11;
  public static final int HCUT=12;
    
  public double width;  // Width  along x-axis
  public double height; // Height along y-axis
  public double depth;  // Depth  along z-axis;
  public Vector extras; // Extra object attached to this object
    
  public Block(String ident,Point3D p0,double width,double height,double depth)
  { super(ident,p0);
    this.width=width;
    this.height=height;
    this.depth=depth;
    this.extras=new Vector();
//    DEBUG.INFO("new Block:"+ident+"  p0="+p0+
//                       ",width="+width+
//                       ",height="+height+
//                       ",depth="+depth
//                      );
  }
  
  // ***************************************************************************
  // *** METHOD: getModel3D
  // ***************************************************************************
  public /*abstract*/ Group getModel3D(Color3f c)
  { TransformedGroup3D tg=new TransformedGroup3D();
    tg.setSize(width,height,depth);
    tg.setP0(p0.x,p0.y,p0.z);
//    tg.setAngles(new Vector3d(1.0,0.0,0.0));
//    tg.setAngleX(Math.PI/2);  
//    tg.setAngleY(Math.PI/2);  
//    tg.setAngleZ(Math.PI/2);  
    Shape3D shape=new Cube();
    Appearance app=Object3D.createAppearance(8,c);
    shape.setAppearance(app);
    tg.addChild(shape);
    return(tg);
  }  
    
  // 3D ==> 2D projections the XZ, XY or ZY-plane
  // 'origo2D' position of origo in 2D coordinates
  public Rectangle2D.Double projection(int direction,Point2D.Double origo2D,double scale)
  { double x1=0,y1=0,w1=0,h1=0;
    switch(direction)
    { case HCUT:     
      case ABOVE: // Horisontial projection seen from above onto the XZ-plane
           x1=origo2D.x+p0.x; y1=origo2D.y+p0.z; w1=width; h1=depth;
           break;
      case BELOW: // Horisontial projection seen from below onto the XZ-pla//    DEBUG.INFO("new Block:"+ident+"  p0="+p0+ne
           x1=origo2D.x+p0.x; y1=origo2D.y-p0.z-height; w1=width; h1=depth;
           break;
//      case VCUT:     
      case SOUTH: // Vertical projection seen from south onto the XY-plane
           x1=origo2D.x+p0.x; y1=origo2D.y-p0.y-height; w1=width; h1=height;
           break;
      case NORTH: // Vertical projection seen from north onto the XY-plane
           x1=origo2D.x-p0.x-width; y1=origo2D.y-p0.y-height; w1=width; h1=height;
           break;
      case VCUT:     
      case EAST: // Vertical projection seen from east onto the ZY-plane
           x1=origo2D.x-p0.z-depth; y1=origo2D.y-p0.y-height; w1=depth; h1=height;
           break;
      case WEST: // Vertical projection seen from west onto the ZY-plane
           x1=origo2D.x+p0.z; y1=origo2D.y-p0.y-height; w1=depth; h1=height;
           break;
    }       
    return(new Rectangle2D.Double(x1*scale,y1*scale,w1*scale,h1*scale));
  }
    
  public void drawProjection(int direction,Graphics2D g,Point2D.Double origo2D,double scale,Color c)
  { Rectangle2D.Double r=projection(direction,origo2D,scale);
    fill(g,r,c); draw(g,r);
  }
   
  public void addExtra(Block extra)
  { this.extras.add(extra); }  
    
  public void drawExtraProjection(int direction,Graphics2D g,Point2D.Double origo2D,double scale,Color c)
  { for(Enumeration e=extras.elements();e.hasMoreElements();)
    { Block extra=(Block)e.nextElement();
      extra.drawProjection(direction,g,origo2D,scale,rammeFarge);
    }
  }
    
  public Block getExtra(String ident)
  { for(Enumeration e=extras.elements();e.hasMoreElements();)
    { Block extra=(Block)e.nextElement();
      if(extra.ident.equals(ident)) return(extra);
    }
    return(null);
  }
}  